package net.sxnic.ugr.user.action;

import java.util.ArrayList;
import java.util.Map;

import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.group.Group;
import net.sxnic.ugr.group.GroupManager;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.role.RoleManager;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * Introduction：用户注册类
 * @author 刘强
 * create date : 2014-3-31
 */

public class Register extends UgrActionSupport {

	private static final long serialVersionUID = 1L;

	
	@Autowired
	private GroupManager groupManager;
    @Autowired
	private DeptManager  deptManager;
    @Autowired
    private RoleManager  roleManager;
	private User user;

	private String confirmPassword;

	public String execute() throws Exception {
            try{
            	 user.setPlainPassword(user.getPassword());
				 user.setPassword(DigestUtils.md5Hex(user.getPassword()));
				 //注册用户默认为申报组
				 Group g =groupManager.findByUnique("group","approver_g");
				 user.getGroups().add(g);
				 //注册用户默认为申报单位或个人
				 Dept dept=deptManager.findByCode("990");
				 user.setDept(dept);
				 //注册用户默认为approver角色
	             Role role = roleManager.findByUnique("role", "approver");
				 user.getRoles().add(role);
				 userManager.save(user);
				 addActionMessage("注册成功!");
            }catch(Exception e){
            	 addActionError("注册失败!");
            	 return INPUT;
            }
		  return SUCCESS;

	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the confirmPassword
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * @param confirmPassword the confirmPassword to set
	 */
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	
}
